package mysql

import (
	log "common/log4go"
	"fmt"
	"iot-bridge/iot_cmd_up/distribute/model"
)

type ReportDataDao struct {
	table string
}

const SaveSql = "INSERT INTO %s (report_type, sn, cid, timestamp, payload, ctime, mtime) VALUES (%d, '%s', '%s', %d, '%s', now(), now())"

func NewReportDataDao() (dao *ReportDataDao, err error) {
	dao = &ReportDataDao{
		table: "report_data",
	}
	return
}
func (this *ReportDataDao) SaveReportDataNew(table string, rd *model.ReportData) (id int64, err error) {
	newSaveSql := fmt.Sprintf(SaveSql, table, rd.ReportType, rd.Sn, rd.Cid, rd.TimeStamp, rd.Payload)
	row, err := db.Exec(newSaveSql)
	if err != nil {
		log.Error(" db.Exec(\" %s\") error(%v)", newSaveSql, err)
		return int64(0), err
	}
	id, err = row.LastInsertId()
	if err != nil {
		log.Error("row.LastInsertId() error(%v)", err)
		return int64(0), err
	}
	return id, nil
}

//func (this *ReportDataDao) SaveReportData(rd *model.ReportData) (id int64, err error) {
//	res, err := this.saveStmt.Exec(&rd.ReportType, &rd.Sn, &rd.Cid, &rd.TimeStamp, &rd.Payload)
//	if err != nil {
//		log.Error("this.saveStmt.Exec(\" %v \") error(%v)", rd, err)
//		return int64(0), err
//	}
//	id, err = res.LastInsertId()
//	if err != nil {
//		log.Error("res.LastInsertId() error(%v)", err)
//		return int64(0), err
//	}
//	return
//}
